package com.sinux.generality.provider.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinux.generality.basesupport.utils.base.BaseController;
import com.sinux.generality.provider.demo.tool.OpenUtil;
import com.sinux.generality.provider.demo.tool.SpringContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RefreshScope
@RestController
@Api(tags = "openai接口")
@RequestMapping("/openai")
public class OpenController extends BaseController {
    @ApiOperation("获取模型列表")
    @GetMapping("/getModels")
    public String getModels(){
        try {
            String str = OpenUtil.getModels();
            return ok(str);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation("建立会话模型")
    @GetMapping("/completions")
    public String completions(String content){
        try {
            String str = OpenUtil.completions(content);
            JSONObject json = JSONObject.parseObject(str);
            String resultStr = json.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
            return ok(resultStr);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
